<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="保单号" prop="contractNumber">
        <el-input
          v-model="queryParams.contractNumber"
          placeholder="请输入保单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发生事故机构" prop="troubleInstitution">
        <el-input
          v-model="queryParams.troubleInstitution"
          placeholder="请输入发生事故机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事故状态" prop="troubleState">
        <el-select v-model="queryParams.troubleState" placeholder="请选择事故状态" clearable>
          <el-option
            v-for="dict in dict.type.trouble_condition"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['insurance:insurance_trouble:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['insurance:insurance_trouble:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['insurance:insurance_trouble:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['insurance:insurance_trouble:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="insurance_troubleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事故ID" align="center" prop="troubleId" />
      <el-table-column label="保单号" align="center" prop="contractNumber" />
      <el-table-column label="发生事故机构" align="center" prop="troubleInstitution" />
      <el-table-column label="事故简述" align="center" prop="troubleResume" />
      <el-table-column label="事故日期" align="center" prop="troubleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.troubleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事故状态" align="center" prop="troubleState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.trouble_condition" :value="scope.row.troubleState"/>
        </template>
      </el-table-column>
      <el-table-column label="事故影响" align="center" prop="troubleImapct" />
      <el-table-column label="直接损失" align="center" prop="lossMoney" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['insurance:insurance_trouble:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['insurance:insurance_trouble:remove']"
          >删除</el-button>
          <el-button
    size="mini"
    type="text"
    icon="el-icon-view"
    @click="handleViewDetails(scope.row)"
    v-hasPermi="['insurance:insurance_trouble::view']"
  >应急措施</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网络安全事故对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="showInsuranceFields" label="保单号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入保单号" />
        </el-form-item>
        <el-form-item v-if="showInsuranceFields" label="发生事故机构" prop="troubleInstitution">
          <el-input v-model="form.troubleInstitution" placeholder="请输入发生事故机构" />
        </el-form-item>
        <el-form-item v-if="showInsuranceFields" label="事故简述" prop="troubleResume">
          <el-input v-model="form.troubleResume" placeholder="请输入事故简述" />
        </el-form-item>
        <el-form-item v-if="showInsuranceFields" label="事故日期" prop="troubleDate">
          <el-date-picker clearable
            v-model="form.troubleDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择事故日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="showInsuranceFields" label="事故状态" prop="troubleState">
          <el-select v-model="form.troubleState" placeholder="请选择事故状态">
            <el-option
              v-for="dict in dict.type.trouble_condition"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showInsuranceFields" label="事故影响" prop="troubleImapct">
          <el-input v-model="form.troubleImapct" placeholder="请输入事故影响" />
        </el-form-item>
        <el-form-item v-if="showInsuranceFields" label="直接损失" prop="lossMoney">
          <el-input v-model="form.lossMoney" placeholder="请输入直接损失" />
        </el-form-item>
        <el-divider content-position="center">应急措施信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddEmergency">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteEmergency">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="emergencyList" :row-class-name="rowEmergencyIndex" @selection-change="handleEmergencySelectionChange" ref="emergency">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="具体措施" prop="concreteMeasure" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.concreteMeasure" placeholder="请输入具体措施" />
            </template>
          </el-table-column>
          <el-table-column label="应急类型" prop="emergencyStyle" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.emergencyStyle" placeholder="请输入应急类型" />
            </template>
          </el-table-column>
          <el-table-column label="响应时间" prop="respondTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.respondTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择响应时间" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInsurance_trouble, getInsurance_trouble, delInsurance_trouble, addInsurance_trouble, updateInsurance_trouble } from "@/api/insurance/insurance_trouble";

export default {
  name: "Insurance_trouble",
  dicts: ['trouble_condition'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedEmergency: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网络安全事故表格数据
      insurance_troubleList: [],
      // 应急措施表格数据
      emergencyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      showInsuranceFields: true, // 控制是否显示保险合同字段
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNumber: null,
        troubleInstitution: null,
        troubleState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractNumber: [
          { required: true, message: "保单号不能为空", trigger: "blur" }
        ],
        troubleInstitution: [
          { required: true, message: "发生事故机构不能为空", trigger: "blur" }
        ],
        troubleResume: [
          { required: true, message: "事故简述不能为空", trigger: "blur" }
        ],
        troubleDate: [
          { required: true, message: "事故日期不能为空", trigger: "blur" }
        ],
        troubleState: [
          { required: true, message: "事故状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询网络安全事故列表 */
    getList() {
      this.loading = true;
      listInsurance_trouble(this.queryParams).then(response => {
        this.insurance_troubleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        troubleId: null,
        contractNumber: null,
        troubleInstitution: null,
        troubleResume: null,
        troubleDate: null,
        troubleState: null,
        troubleImapct: null,
        lossMoney: null
      };
      this.emergencyList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.troubleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网络安全事故";
    },
     /** 查看详情按钮操作 */
     handleViewDetails(row) {
      this.reset();
      const troubleId = row.troubleId || this.ids
      getInsurance_trouble(troubleId).then(response => {
        this.form = response.data;
        this.emergencyList = response.data.emergencyList;
        this.open = true;
        this.showInsuranceFields=false;
        this.title = "查看应急措施";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const troubleId = row.troubleId || this.ids
      getInsurance_trouble(troubleId).then(response => {
        this.form = response.data;
        this.emergencyList = response.data.emergencyList;
        this.open = true;
        this.showInsuranceFields=true;
        this.title = "修改网络安全事故";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.emergencyList = this.emergencyList;
          if (this.form.troubleId != null) {
            updateInsurance_trouble(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInsurance_trouble(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const troubleIds = row.troubleId || this.ids;
      this.$modal.confirm('是否确认删除网络安全事故编号为"' + troubleIds + '"的数据项？').then(function() {
        return delInsurance_trouble(troubleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 应急措施序号 */
    rowEmergencyIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 应急措施添加按钮操作 */
    handleAddEmergency() {
      let obj = {};
      obj.concreteMeasure = "";
      obj.emergencyStyle = "";
      obj.respondTime = "";
      this.emergencyList.push(obj);
    },
    /** 应急措施删除按钮操作 */
    handleDeleteEmergency() {
      if (this.checkedEmergency.length == 0) {
        this.$modal.msgError("请先选择要删除的应急措施数据");
      } else {
        const emergencyList = this.emergencyList;
        const checkedEmergency = this.checkedEmergency;
        this.emergencyList = emergencyList.filter(function(item) {
          return checkedEmergency.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleEmergencySelectionChange(selection) {
      this.checkedEmergency = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('insurance/insurance_trouble/export', {
        ...this.queryParams
      }, `insurance_trouble_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
